<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/12
 * Time: 18:21
 */

namespace Application\Models;


use Phalcon\Mvc\Model;

class BookCategory extends Model
{
    public function initialize()
    {
        $this->setSource("learn_kancloud_category");
    }

    public function getOptionList($default = 0)
    {
        $list = $this->getCategoryList();
        $string = '';
        foreach ($list as $key => $value) {
            if ($default == $value['id'])
                $se = 'selected = "selected"';
            else
                $se = '';

            $prex = str_repeat('&emsp;', $value['level'] - 1) . '┝ ';
            $string .= '<option value="' . $value['id'] . '" ' . $se . '>' . $prex . $value['name'] . '</option>';
        }
        return $string;
    }

    public function getCategoryList($id = 0, $level = 1)
    {
        $_list = [];
        $listBox = self::find(['pid=' . $id])->toArray();
        foreach ($listBox as $key => $value) {
            $value['level'] = $level;
            $_list[] = $value;
            $_list = array_merge($_list, $this->getCategoryList($value['id'], $level + 1));
        }
        return $_list;
    }
}